package energyware.ws;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;


public class WSManager {
	

	private String NAMESPACE;
	private String URL;
	private String SOAPACTION;
	private String METHOD;
	
	private SoapSerializationEnvelope envelope;
	private SoapObject request;
	private HttpTransportSE androidHttpTransport;
	private String response;
	
	private String TAG;
	
	public WSManager (String METHOD, String TAG){
		this.NAMESPACE = "http://s411925227.onlinehome.us/soap/EnerService";
		this.URL = "http://s411925227.onlinehome.us/Server/Server.php";
		this.SOAPACTION = "http://s411925227.onlinehome.us/Server/Server.php/" + METHOD;
		this.METHOD = METHOD;
		this.request = new SoapObject(NAMESPACE, this.METHOD);
		this.envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = false;
		this.androidHttpTransport = new HttpTransportSE(URL);
		this.TAG = TAG;
	}
	
	public String authenticateUser(String email, String password){
		
        PropertyInfo userNameSoap = new PropertyInfo();
        userNameSoap.setName("email");
        userNameSoap.setValue(email);
        userNameSoap.setType(String.class);
        
        request.addProperty(userNameSoap);
        
        PropertyInfo passwordSoap = new PropertyInfo();
        passwordSoap.setName("password");
        passwordSoap.setValue(password);
        passwordSoap.setType(String.class);
        
        request.addProperty(passwordSoap);
        
        envelope.setOutputSoapObject(request);
        
        try
        {
        	androidHttpTransport.call(SOAPACTION, envelope);
        	response = envelope.getResponse().toString();
        	
        	return response;

        }catch(Exception e)
        {
        	Log.e(TAG, e.getMessage());
        	return response;
        }
	}

	public String queryUserRecovery(String email){
	        
        PropertyInfo emailSoap = new PropertyInfo();
        emailSoap.setName("email");
        emailSoap.setValue(email);
        emailSoap.setType(String.class);
        
        request.addProperty(emailSoap);
        
        
        envelope.setOutputSoapObject(request);
    
        try
        {
                androidHttpTransport.call(SOAPACTION, envelope);
                response = envelope.getResponse().toString();
                
                return response;

        }catch(Exception e)
        {
                Log.e(TAG, e.getMessage());
                return response;
        }
     }
	
	public String getAllQuestions(){
		
		PropertyInfo userNameSoap = new PropertyInfo();
        userNameSoap.setName("name");
        userNameSoap.setValue("getAllQuestions");
        userNameSoap.setType(String.class);
        
        request.addProperty(userNameSoap);
        
        envelope.setOutputSoapObject(request);
    
        try
        {
                androidHttpTransport.call(SOAPACTION, envelope);
                response = envelope.getResponse().toString();
                
                return response;

        }catch(Exception e)
        {
                Log.e(TAG, e.getMessage());
                return response;
        }
     }
	
	public String registerUser (String password, String email, String names, int question, String answer){
		
        
        PropertyInfo passwordSoap = new PropertyInfo();
        passwordSoap.setName("password");
        passwordSoap.setValue(password);
        passwordSoap.setType(String.class);
        
        request.addProperty(passwordSoap);
        
        PropertyInfo emailSoap = new PropertyInfo();
        emailSoap.setName("email");
        emailSoap.setValue(email);
        emailSoap.setType(String.class);
        
        request.addProperty(emailSoap);
        
        
        PropertyInfo namesSoap = new PropertyInfo();
        namesSoap.setName("names");
        namesSoap.setValue(names);
        namesSoap.setType(String.class);
        
        request.addProperty(namesSoap);
        
        
        PropertyInfo questionSoap = new PropertyInfo();
        questionSoap.setName("question");
        questionSoap.setValue(question);
        questionSoap.setType(int.class);
        
        request.addProperty(questionSoap);
        
        PropertyInfo answerSoap = new PropertyInfo();
        answerSoap.setName("answer");
        answerSoap.setValue(answer);
        answerSoap.setType(String.class);
        
        request.addProperty(answerSoap);
        
        envelope.setOutputSoapObject(request);
        
        try
        {
        	androidHttpTransport.call(SOAPACTION, envelope);
        	response = envelope.getResponse().toString();
        	
        	return response;

        }catch(Exception e)
        {
        	Log.e(TAG, e.getMessage());
        	return response;
        }
		
	}
	
	public String getEmail(String email){
        
        PropertyInfo emailSoap = new PropertyInfo();
        emailSoap.setName("email");
        emailSoap.setValue(email);
        emailSoap.setType(String.class);
        
        request.addProperty(emailSoap);
        
        
        envelope.setOutputSoapObject(request);
    
        try
        {
                androidHttpTransport.call(SOAPACTION, envelope);
                response = envelope.getResponse().toString();
                
                return response;

        }catch(Exception e)
        {
                Log.e(TAG, e.getMessage());
                return response;
        }
     }
		
	public String getidUserFromUsuario(String email){
        
        PropertyInfo emailSoap = new PropertyInfo();
        emailSoap.setName("email");
        emailSoap.setValue(email);
        emailSoap.setType(String.class);
        
        request.addProperty(emailSoap);
        
        
        envelope.setOutputSoapObject(request);
    
        try
        {
                androidHttpTransport.call(SOAPACTION, envelope);
                response = envelope.getResponse().toString();
                
                return response;

        }catch(Exception e)
        {
                Log.e(TAG, e.getMessage());
                return response;
        }
     }	

	public String getConsumption (String id, int state, String date){
		
		    PropertyInfo idSoap = new PropertyInfo();
		    idSoap.setName("id");
		    idSoap.setValue(id);
		    idSoap.setType(String.class);
	        
	        request.addProperty(idSoap);
	        
	        
	        PropertyInfo stateSoap = new PropertyInfo();
	        stateSoap.setName("state");
	        stateSoap.setValue(state);
	        stateSoap.setType(Integer.class);
	        
	        request.addProperty(stateSoap);
	        
	        PropertyInfo dateSoap = new PropertyInfo();
	        dateSoap.setName("date");
	        dateSoap.setValue(date);
	        dateSoap.setType(String.class);
	        
	        request.addProperty(dateSoap);
	        
	        envelope.setOutputSoapObject(request);
	        
		
		    try
	        {
                androidHttpTransport.call(SOAPACTION, envelope);
                response = envelope.getResponse().toString();
	                
                return response;

	        }catch(Exception e)
	        {
	                Log.e(TAG, e.getMessage());
	                return response;
	        }
	}

	public String getComputers (String email){
		
		
			PropertyInfo emailSoap = new PropertyInfo();
			emailSoap.setName("email");
			emailSoap.setValue(email);
			emailSoap.setType(String.class);
	        
	        request.addProperty(emailSoap);
	        
	        envelope.setOutputSoapObject(request);
	        
			
		    try
	        {
                androidHttpTransport.call(SOAPACTION, envelope);
                response = envelope.getResponse().toString();
	                
                return response;

	        }catch(Exception e)
	        {
	                Log.e(TAG, e.getMessage());
	                return response;
	        }
	}
	
	public String insertLatAndLong (String id, String lat, String lon){
		
		
		PropertyInfo idSoap = new PropertyInfo();
		idSoap.setName("id");
		idSoap.setValue(id);
		idSoap.setType(Integer.class);
        
        request.addProperty(idSoap);
        
        PropertyInfo latSoap = new PropertyInfo();
        latSoap.setName("lat");
        latSoap.setValue(lat);
        latSoap.setType(String.class);
        
        request.addProperty(latSoap);
        
        PropertyInfo lonSoap = new PropertyInfo();
        lonSoap.setName("lon");
        lonSoap.setValue(lon);
        lonSoap.setType(String.class);
        
        request.addProperty(lonSoap);
        
        envelope.setOutputSoapObject(request);
        
		
	    try
        {
            androidHttpTransport.call(SOAPACTION, envelope);
            response = envelope.getResponse().toString();
                
            return response;

        }catch(Exception e)
        {
                Log.e(TAG, e.getMessage());
                return response;
        }
}
	
	public String changeComputerAlias (String id, String alias){
		
		
		PropertyInfo idSoap = new PropertyInfo();
		idSoap.setName("id");
		idSoap.setValue(id);
		idSoap.setType(Integer.class);
        
        request.addProperty(idSoap);
        
        PropertyInfo aliasSoap = new PropertyInfo();
        aliasSoap.setName("alias");
        aliasSoap.setValue(alias);
        aliasSoap.setType(String.class);
        
        request.addProperty(aliasSoap);
        
        
        
        envelope.setOutputSoapObject(request);
        
		
	    try
        {
            androidHttpTransport.call(SOAPACTION, envelope);
            response = envelope.getResponse().toString();
                
            return response;

        }catch(Exception e)
        {
                Log.e(TAG, e.getMessage());
                return response;
        }
}

	public String suspendComputer(String id, String state){
	PropertyInfo idSoap = new PropertyInfo();
	idSoap.setName("id");
	idSoap.setValue(id);
	idSoap.setType(String.class);
    
    request.addProperty(idSoap);
    
    PropertyInfo stateSoap = new PropertyInfo();
    stateSoap.setName("state");
    stateSoap.setValue(state);
    stateSoap.setType(String.class);
    
    request.addProperty(stateSoap);
    
    
    
    envelope.setOutputSoapObject(request);
    
	
    try
    {
        androidHttpTransport.call(SOAPACTION, envelope);
        response = envelope.getResponse().toString();
            
        return response;

    }catch(Exception e)
    {
            Log.e(TAG, e.getMessage());
            return response;
    }
}

	public String suspendState(String id){
	PropertyInfo idSoap = new PropertyInfo();
	idSoap.setName("id");
	idSoap.setValue(id);
	idSoap.setType(String.class);
    
    request.addProperty(idSoap);    
    
    
    envelope.setOutputSoapObject(request);
    
	
    try
    {
        androidHttpTransport.call(SOAPACTION, envelope);
        response = envelope.getResponse().toString();
            
        return response;

    }catch(Exception e)
    {
            Log.e(TAG, e.getMessage());
            return response;
    }
}

}
